// pages/address-add/index.js
const WXAPI=require("../../miniprogarm_npm/apifm-wxapi/index.js")
const CONFIG=require("../../config.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    linkMan:"",
    address:"",
    mobile:"",
    proArray:[],//省的数组
    pindex:0,//省的下标
    cityArray:[],//市的数组
    cityIndex:0,//市的下标
    areaArray:[],//区的数据
    areaIndex:0,
    addressInfo:{},
    addressId:null,//修改和删除使用的id
  },
  getLinkMan(e){
    console.log(e)
    this.setData({
      linkMan:e.detail.value
    })
  },
  getMobile(e){
    this.setData({
      mobile:e.detail.value
    })
  },
  getAddress(e){
    this.setData({
      address:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad (options) {
    WXAPI.init(CONFIG.subDomain)
    // 如果有id直接加载地址
    if(options.id){
      //     getEditAddress 获取要修改的地址  返回的数据有省 市  区 的id 
         const res=await WXAPI.getEditAddress(options.id,wx.getStorageSync('token'))
          console.log(res)
        const proRes=await  WXAPI.getProvince()
        const ind=proRes.data.findIndex(ele=>{
          return ele.id==res.data.info.provinceId  // res.data.info.provinceId省id
        })
        console.log(ind)
        this.setData({
          pindex:ind,
          proArray:proRes.data,
          addressId:options.id
        })
        // 请求市
        const cityres=await WXAPI.getCityAndArea(res.data.info.provinceId)
        console.log(cityres)
        var citind=cityres.data.findIndex(ele=>{
          return ele.id==res.data.info.cityId
        })
        console.log(citind)
        this.setData({
          cityArray:cityres.data,
          cityIndex:citind
        })
        // 请求区
        const areares=await WXAPI.getCityAndArea(res.data.info.cityId)
        console.log(areares)
        var areaInd=areares.data.findIndex(ele=>{
          return ele.id==res.data.info.districtId
        })
        this.setData({
          areaIndex:areaInd,
          areaArray:areares.data,
          linkMan:res.data.info.linkMan,
          address:res.data.info.address,
          mobile:res.data.info.mobile
        })

    }
  },
  async delAddress(){  //删除地址
      var res=await WXAPI.delAddress(wx.getStorageSync('token'),this.data.addressId)
      console.log(res)
      if(res.code==0){
        wx.navigateBack({})
      }
  },
  async getProvince(){
    var res=await WXAPI.getProvince()
    console.log(res)
    const province=[{
      id:0,
      name:"请选择"
    }].concat(res.data)
    this.setData({
      proArray:province
    })

  },
  async bindPickerProChange(e){  //省的改变
      console.log(e)
      this.setData({
        pindex:e.detail.value  //下标
      })
      var pid=this.data.proArray[e.detail.value].id
      const res=await WXAPI.getCityAndArea(pid)
      console.log(res)
      const city=[{id:0,name:"请选择"}].concat(res.data)
      console.log(city)
      this.setData({
        cityArray:city
      })
  },
  async bindPickerCityChange(e){ //市的改变事件
      this.setData({
        cityIndex:e.detail.value
      })
    const pid=this.data.cityArray[e.detail.value].id
      const res=await WXAPI.getCityAndArea(pid)
      console.log(res)
      const area=[{id:0,name:"请选择"}].concat(res.data)
      this.setData({
        areaArray:area
      })
  },
  bindPickerChange(e){
    this.setData({
      areaIndex:e.detail.value
    })
  },
  async addAddress(){//添加地址接口
      var res=await WXAPI.addAddress({
        linkMan:this.data.linkMan,
        address:this.data.address ,
        mobile: this.data.mobile,
       token: wx.getStorageSync('token'),
        cityId: this.data.cityArray[this.data.cityIndex].id,
        provinceId: this.data.proArray[this.data.pindex].id,
        districtId:this.data.areaArray[this.data.areaIndex].id,
        isDefault: false, //是否默认地址
        code: 322000,
      })
      console.log(res)
        if(res.code==0){
          wx.navigateBack({})
        }
  },
  getwxAddress(){
    // 获取授权列表
    wx.getSetting({
      success(res) {
            // 授权
            wx.authorize({
              scope: 'scope.address',
              success () {
                // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
                wx.chooseAddress({
                  success (res) {
                    console.log(res)
                    console.log(res.userName)
                    console.log(res.postalCode)
                    console.log(res.provinceName)
                    console.log(res.cityName)
                    console.log(res.countyName)
                    console.log(res.detailInfo)
                    console.log(res.nationalCode)
                    console.log(res.telNumber)
                  }
                })
              }
            })
          
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.getProvince()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})